<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>   
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>  
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>  
<%@ taglib uri="http://shiro.apache.org/tags" prefix="shiro" %>     
<c:set var="ctx" value="${pageContext.request.contextPath}"/> 
<%@ taglib prefix="dict" uri="/WEB-INF/tld/dict.tld" %>  
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<jsp:include page="../include.jsp"/>
<script type="text/javascript">
	var dataGrid;
	$(function() {
		itemsShift = <dict:getDictItem dictCode="DICT_WORK_SHIFT"/>;
		setHeight();
		dataGrid = $('#dg').datagrid({
			title:'机台运行状态报表',
			url:'${ctx}/machRunTime/listMachRunTimes',
			nowrap:true,
			striped:true,
			idField:'pmId',
			pagination:true,
			fit:true,
			fitColumns:true,
			sortName:'PRO_DATE,MACH_NUM',
			sortOrder:'desc,asc',
			rownumbers: true, //是否加行号 
			columns:[[	   
				 {field:'mrtId',checkbox :true},
				 {field:'proDate',title:'生产日期',width:50,align:'left',formatter:formatterProDate},
	    		 {field:'machNum',title:'机台号',width:50,align:'left'},	
	    		 {field:'shift',title:'班次',width:30,align:'left',formatter:formatterShift},	
	    		 {field:'runTime',title:'运行时长(秒)',width:50,align:'left',styler:cellStylerRun},
	    		 {field:'standBy',title:'停止时长(秒)',width:50,align:'left',styler:cellStylerStand},
	    		 {field:'alarm',title:'报错时长(秒)',width:50,align:'left',styler:cellStylerAlarm},
	    		 {field:'offLine',title:'离线时长(秒)',width:50,align:'left',styler:cellStylerOff}
			]],
			toolbar : '#toolbar',
			onHeaderContextMenu: function(e, field){
                e.preventDefault();
                $(this).datagrid('columnMenu').menu('show', {
                    left:e.pageX,
                    top:e.pageY
                });
            }
		}).datagrid('columnMoving');
		
	});
	
    function cellStylerRun(value,row,index){
    	return 'background-color:green;';
   	}
    function cellStylerStand(value,row,index){
    	return 'background-color:#eef105;';
   	}
    function cellStylerAlarm(value,row,index){
    	return 'background-color:red;';
   	}
    function cellStylerOff(value,row,index){
    	return 'background-color:gray;';
   	}
    
	function doSearch(){
		var params=$('#queryForm').serializeObject();
		$('#dg').datagrid('load',params);  
	}
	
	function doClear(){
		$('#queryForm').form("clear");
	}
	
	
	function formatterProDate(value,row,index){
		if(value){
			return value.substr(0,10);
		}
		return value;
	}
	
	function formatterShift(value, row, index){
   		var val =  $.getDictItemText(itemsShift,value);
   		return val;
   	}
	
	function setHeight(){
		var c = $('#cc');
		var p = c.layout('panel','north');	// get the north panel
		var oldHeight = p.panel('panel').outerHeight();
		p.panel('resize', {height:'auto'});
		var newHeight = p.panel('panel').outerHeight();
		c.layout('resize',{
			height: (c.height() + newHeight - oldHeight)
		});
	}
	
    function getDaysBetween(dateString1,dateString2){
   	   var  startDate = Date.parse(dateString1);
   	   var  endDate = Date.parse(dateString2);
   	   var days=(endDate - startDate)/(1*24*60*60*1000);
   	   return  days;
   	}
	
	function exportExcel(){
		var proDateFrom = $('#proDateFrom').datebox('getValue');
		var proDateTo = $('#proDateTo').datebox('getValue');
		if(proDateFrom==""){
			parent.$.messager.alert('错误', "请选择一个起始日期！", 'error');
			return;
		}
		if(proDateTo!=""){
			var days = getDaysBetween(proDateFrom,proDateTo);
			if(days >30){
				parent.$.messager.alert('错误', "时间跨度不能超过30天！", 'error');
				return;
			}
		}
		if(proDateTo==""){
			var days = getDaysBetween(proDateFrom,new Date());
			if(days >30){
				parent.$.messager.alert('错误', "起始时间不能是30天以前！", 'error');
				return;
			}
		}
		var machNum = $('#machNum').textbox('getValue');
		var shift = $('#shift').combobox('getValue');
		location.href="${ctx}/machRunTime/download?proDateFrom="+proDateFrom+"&proDateTo="+proDateTo+"&machNum="+machNum+"&shift="+shift;
	}
</script>
</head>
<body>
	<div id="cc" class="easyui-layout" data-options="fit:true">
	    <div data-options="region:'north',border:false" style="height:50px;padding-top:10px;padding-left:5px">	        
	        <form id="queryForm" method="post">			
				<div style="margin-bottom:1px;padding-left:5px">
					<input class="easyui-textbox" id="machNum" name="machNum" labelAlign="right" labelWidth="100px" label="机台号:" labelPosition="left" data-options="" style="width:20%;height:22px;">
					<input class="easyui-datebox" id="proDateFrom" name="proDateFrom" labelAlign="right" label="起始生产日期:" labelWidth="100px" labelPosition="left" data-options="" style="width:20%;height:22px;">
					<input class="easyui-datebox" id="proDateTo"  name="proDateTo"  labelAlign="right" label="截止生产日期:" labelWidth="100px" style="width:20%;height:22px;" >
					<select class="easyui-combobox" id="shift" name="shift" labelAlign="right" labelWidth="100px" label="班次:"labelPosition="left" editable="false" style="width:20%;height:24px;">
						<option value="">全部</option>							
						<c:forEach items="${dictShiftItems}" var="item">
							<option value="${item.itemValue}" >${item.itemText}</option>
						</c:forEach>
					</select>
					<a onclick="doSearch();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-search'">查询</a>
					<a onclick="doClear();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-reload'">重置</a>				
				</div>
			</form>
	    </div>
		<div data-options="region:'center',border:false">
		     <table id="dg"></table>
		 </div>
	</div>
	
	<div id="toolbar" style="display: none;">
		<a onclick="exportExcel();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-excel'">导出</a>		
	</div>
</body>
</html>